package com.demo.checkboxdemoproject;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hexiang on 17/8/14.
 */

public class Adapter extends BaseAdapter {
    private LayoutInflater inflater;
    private List<ItemBean> items;
    private ItemBean item;
    private OnShowItemClickListener onShowItemClickListener;
    Context mContext;

    // 存储勾选框状态的map集合
    private Map<Integer, Boolean> map = new HashMap<>();


    private CheckedItem checkedItem;

    public Adapter(List<ItemBean> list, Context context) {
        items = list;
        inflater = LayoutInflater.from(context);
        this.mContext = context;
        initMap();
    }

    @Override
    public int getCount() {
        // TODO 自动生成的方法存根
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO 自动生成的方法存根
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO 自动生成的方法存根
        return items.get(position).getId();
    }

    //初始化map集合,默认为不选中
    private void initMap() {
        for (int i = 0; i < items.size(); i++) {
            map.put(i, false);
        }
    }


    //返回集合给Activity
    public Map<Integer, Boolean> getMap() {
        return map;
    }


    public int getNumber() {

        List<Integer> cont = new ArrayList<>();


        for (int key : map.keySet()) {

            boolean value = map.get(key);

            if (value == true) {
                cont.add(key);
            }

        }

        return cont.size();
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO 自动生成的方法存根
        ViewHolder holder;//使用ViewHolder，大神说能提升性能
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item, null);
            holder.cb = (CheckBox) convertView.findViewById(R.id.checkBox1);
            holder.title = (TextView) convertView.findViewById(R.id.title_tv);
            holder.unmber = (TextView) convertView.findViewById(R.id.unmber_tv);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        item = items.get(position);
        if (item.isShow()) {
            holder.cb.setVisibility(View.VISIBLE);
        } else {
            holder.cb.setVisibility(View.GONE);
        }


        holder.unmber.setText(item.getTitle());


            holder.title.setText((position + 1) + "");




        holder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                map.put(position, isChecked);

                if (isChecked) {
                    item.setChecked(true);
                } else {
                    item.setChecked(false);
                }
                //回调方法，讲item加入已选择的
                onShowItemClickListener.onShowItemClick(item, position, items);

            }
        });
        //监听后设置选择状态
        holder.cb.setChecked(item.isChecked());
        return convertView;
    }

    static class ViewHolder {
        CheckBox cb;
        TextView title;
        TextView unmber;

    }

    public interface OnShowItemClickListener {
        public void onShowItemClick(ItemBean bean, int position, List<ItemBean> items);
    }

    public void setOnShowItemClickListener(OnShowItemClickListener onShowItemClickListener) {
        this.onShowItemClickListener = onShowItemClickListener;

    }


    public interface CheckedItem {
        void checkedPissonItem(int position, Map<Integer, Boolean> map, boolean isChecked);
    }


    public void setListen(CheckedItem checkedItem) {
        this.checkedItem = checkedItem;

    }


}